package swu.cx.test

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.graphics.toRect
import androidx.core.graphics.toRectF

class downLoad:View {
    //覆盖矩形的宽度
    private var mutableWidth=0f
    //记录圆角矩形的x半径
    private var mrx=0f
    //记录圆角矩形的y半径
    private var mry=0f
    //记录圆角矩形的左侧
    private var mleft=0f
    //设置圆的半径
    private var radius=0f
    //记录圆心位置
    private var cx=0f
    private var cy=0f
    //记录中央正方形的边长
    private var halfLenth=0f
    //记录常量1
    private var constantVal1=0f
    //记录常量2
    private var constantVal2=0f

    //记录路径
    private val mpath=Path()
    //中央小区域
    private val centerRect=RectF()
    //创建圆角矩形区域
    private val mrect= RectF()

    //记录动画集
    private val allAnims=AnimatorSet()
    private var coverAnim:ValueAnimator?=null
    private var roundCornerAnim:ValueAnimator?=null
    private var cornerMoveAnim:ValueAnimator?=null

    //原画笔
    private val mPaint= Paint().apply {
        color=Color.MAGENTA
        style=Paint.Style.FILL
    }
    //画√的笔
    private val linePaint=Paint().apply {
        color=Color.WHITE
        style=Paint.Style.STROKE
        strokeWidth=3f
    }
    //覆盖画笔
    private val mcoverPaint=Paint().apply {
        color=Color.BLUE
        style=Paint.Style.FILL
    }

    //代码创建
    constructor(context: Context):super(context){}
    //xml创建
    constructor(context: Context, attriset: AttributeSet):super(context,attriset){}
    //有样式的
    constructor(context: Context, attriset: AttributeSet, style:Int):super(context,attriset,style){}

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mrect.apply {
            left=0f
            top=0f
            right=width.toFloat()
            bottom=height.toFloat()
        }
        cx=width/2f
        cy=height/2f
    }
    override fun onDraw(canvas: Canvas?) {
        canvas?.drawRect(mrect,mPaint)
        canvas?.drawRoundRect(mleft,0f,mutableWidth,height.toFloat(),mrx,mry,mcoverPaint)
        canvas?.drawCircle(cx,cy,radius,mcoverPaint)
        canvas?.drawPath(mpath.apply {
            moveTo(cx-halfLenth/2f,cy)
            lineTo(cx-halfLenth/4f,cy+halfLenth/2f)
            lineTo(cx+halfLenth/2f,cy-halfLenth/4f)
        },linePaint)
        canvas?.drawRect(centerRect,mcoverPaint)
    }
    fun createAnim(){
        coverAnim=ValueAnimator.ofFloat(0f, width.toFloat()).apply {
            duration = 1000
            addUpdateListener {
                mutableWidth = it.animatedValue as Float
                if (mutableWidth == width.toFloat()) {
                    mrect.right=0f
                    constantVal1=mutableWidth
                }
                invalidate()
            }
        }
        roundCornerAnim=ValueAnimator.ofFloat(0f,width/5.5f).apply {
            duration = 800
            addUpdateListener {
                mrx = it.animatedValue as Float
                mry=mrx
                invalidate()
            }
        }
        cornerMoveAnim=ValueAnimator.ofFloat(0f,width/2f).apply {
            duration=800
            addUpdateListener {
                mleft=it.animatedValue as Float
                mutableWidth = constantVal1-mleft
                if (it.animatedValue as Float>width/2f-mrx) {
                    radius = height / 2f
                    halfLenth = radius * 1.414f / 2f
                    centerRect.apply {
                        left = cx - halfLenth
                        top = cy - halfLenth
                        right = cx + halfLenth
                        bottom = cy + halfLenth
                    }
                    constantVal2=centerRect.left
                }
                invalidate()
            }
        }
        allAnims.playSequentially(coverAnim,roundCornerAnim,cornerMoveAnim)
        allAnims.addListener(object :Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
            }
            override fun onAnimationEnd(animation: Animator?) {
                ValueAnimator.ofFloat(0f,halfLenth*2).apply {
                    duration=2000
                    addUpdateListener {
                        centerRect.left=(it.animatedValue as Float)+constantVal2
                        invalidate()
                    }
                    start()
                }
            }
            override fun onAnimationCancel(animation: Animator?) {
            }
            override fun onAnimationRepeat(animation: Animator?) {
            }
        })
    }
    fun startAnim(){
        createAnim()
        allAnims.start()
    }
}